#ifndef FORMMAIN_H
#define FORMMAIN_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QSettings>
#include <QTimer>
#include <QLabel>
#include <QLabel>
#include <QPushButton>
#include <QProgressBar>
#include <QClipboard>

#include "ui_formmain.h"
#include "rssitem.h"
#include "rssfeed.h"
#include "rssreader.h"
#include "appfactory.h"
#include "uifactory.h"
#include "lineedit.h"
#include "timer.h"
#include "systemtrayicon.h"
#include "rssthread.h"


namespace Ui {
    class FormMain;
}

class FormMain : public QMainWindow {

        Q_OBJECT
    public:
        explicit FormMain(QWidget *parent = 0);
        ~FormMain();
        void setupTray();
        void setupTrayMenu(QSystemTrayIcon *tray);
        void setupApp();
        void switchWindow();

        void saveSettings();
        void loadSettings();

        static void expandTreeWidget(QTreeWidget *widget);

	protected:
		void dragEnterEvent(QDragEnterEvent *event);
		void dropEvent(QDropEvent *event);
		void keyPressEvent(QKeyEvent *event);

    private:
        Ui::FormMain *ui;
        RssReader *reader;
        SystemTrayIcon *tray;
        Timer *timer;
        QProgressBar *bar;
        QLabel *statusText;
        LineEdit *txtSearch;
		QWidget *spacerWidget;

        QIcon trayHasntNew;
        QFont trayFont;
        QPen trayPen;
        QPainter trayPainter;
        QRect trayRectangle;

		UpdateThread *updater;

        void createConnections();
        void createTray();

	public slots:
        void quit();
        void showWindow();
        void showDialogAbout();
		void showDialogSettings();
        void trayActivated(QSystemTrayIcon::ActivationReason);

        bool isFeedSelected();
        bool isCategorySelected();
		bool isMessageSelected();

		void checkForUpdate();

		// Updates count and state of update feed in feed list.
		void updateDisplay(int index, const QString &title);

		// Hides progressbar and enables Quit action again.
		void hideStatus();

		// Enables progress bar and associated label.
		void showStatus(int barMax);

		void updateListTray();

		void shortcutFeeds(QKeyEvent *event);
		void shortcutMessages(QKeyEvent *event);
		void contextMenuFeed(QMouseEvent *event);
		void contextMenuMessage(QMouseEvent *event);
		void addFeed(const QString &link = tr("http://www.example.com/feed.xml"));
        void deleteFeed();
		void trashItem();
		void deleteMessage();
		void feedSelectionChanged(const QPoint &where = QPoint());

		void openNewTabMsg();
		void openNewTabPage();
		void openNewBrowser();
        void copyIntoClipboard();

        // Opens message in default web view.
		void displayMessage(QTreeWidgetItem *current, QTreeWidgetItem *previous);

        // Opens messages either in new tab or in external browser.
		void expandMessage();

        // Opens given messages in external browser.
		void goToLink(const QStringList &links);

        void editItem();
        void markRead();
        void markUnread();
        void markAllRead();
        void markAllUnread();
        void markCategoryRead();
        void markCategoryUnread();

        void importFeeds();
        void exportFeeds();

        void setReaderPath();

        void filterMessages();
        void deleteMessages();
        void restoreMessages();
        void trashMessages();

        void timerTick();
        void refreshTrayIcon();
        void refreshTrayTooltip();
        void refreshList();

        void updateFeed(RssFeed *feed);
        void updateFeed();
        void updateAllFeeds();
        void updateCategory();

        void evaluateNetCodeError(QNetworkReply::NetworkError code);

        void closeTab(int index);

    signals:
        void listNeedsRefresh();
        void trayNeedsRefresh();

};


#endif // FORMMAIN_H
